package com.imooc.mall.model.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.mall.model.common.Constant;
import com.imooc.mall.model.dao.ProductMapper;
import com.imooc.mall.model.exception.ImoocMallException;
import com.imooc.mall.model.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.Product;
import com.imooc.mall.model.query.ProductListQuery;
import com.imooc.mall.model.request.AddProductReq;
import com.imooc.mall.model.request.ProductListReq;
import com.imooc.mall.model.service.CategoryService;
import com.imooc.mall.model.service.ProductService;
import com.imooc.mall.model.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    CategoryService categoryService;

    @Override
    public void add(AddProductReq addProductReq){
        Product product=new Product();
        BeanUtils.copyProperties(addProductReq,product);
        Product productOld = productMapper.selectByName(addProductReq.getName());
        if(productOld!=null){
            throw new ImoocMallException(ImoocMallExceptionEnum.NEED_EXITSTED);
        }
        int count = productMapper.insertSelective(product);
        if(count==0){
            throw new ImoocMallException(ImoocMallExceptionEnum.INSERT_FAILED);
        }
    }

    @Override
    public void update(Product product){
        Product productOld = productMapper.selectByName(product.getName());
        //同名且不同id，不可以修改
        if(productOld!=null && !product.getId().equals(productOld.getId())){
            throw new ImoocMallException(ImoocMallExceptionEnum.NEED_EXITSTED);
        }
        int count = productMapper.updateByPrimaryKeySelective(product);
        if(count==0){
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }
    }

    @Override
    public void delete(Integer id){
        Product productOld = productMapper.selectByPrimaryKey(id);
        //同名且不同id，不可以修改
        if(productOld==null ){
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
        int count = productMapper.deleteByPrimaryKey(id);
        if(count==0){
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
    }

    @Override
    public void batchUpdateSellStatus(Integer[] ids,  Integer sellStatus){
        productMapper.batchUpdateSellStatus(ids,sellStatus);
    }

    @Override
    public PageInfo     listForAdmin(Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Product> products = productMapper.listForAdmin();
        PageInfo pageInfo = new PageInfo(products);
        return pageInfo;
    }

    @Override
    public Product detail(Integer id){
        Product product = productMapper.selectByPrimaryKey(id);
        return product;
    }

    @Override
    public PageInfo list(ProductListReq productListReq){
        ProductListQuery productListQuery = new ProductListQuery();
        //搜索处理
        if(!StringUtils.isEmpty(productListReq.getKeyWord())){
            String keyWord=new StringBuffer().append("%").append(productListReq.getKeyWord()).append("%").toString();
            productListQuery.setKeyWord(keyWord);
        }
        //目录处理，查某个水果，子目录都查出来，拿到一个目录id的list
        if(productListReq.getCategoryId()!=null){
            List<CategoryVo> categoryVosList = categoryService.listForCustomer(productListReq.getCategoryId());
            ArrayList<Integer> categoryIds = new ArrayList<>();
            categoryIds.add(productListReq.getCategoryId());
            getCategoryIds(categoryVosList,categoryIds);
            productListQuery.setCategoryIds(categoryIds);
        }
        String orderBy = productListReq.getOrderBy();
        if(Constant.ProductListOrderBy.PRICE_DESC_ASC.contains(orderBy)){
            PageHelper.startPage(productListReq.getPageNum(),productListReq.getPageSize(),orderBy);
        }else {
            PageHelper.startPage(productListReq.getPageNum(),productListReq.getPageSize());
        }
        List<Product> productList = productMapper.list(productListQuery);
        PageInfo pageInfo = new PageInfo(productList);
        return pageInfo;
    }

    private void getCategoryIds(List<CategoryVo> categoryVosList,ArrayList<Integer> categoryIds){
        for (int i = 0; i < categoryVosList.size(); i++) {
            CategoryVo categoryVo = categoryVosList.get(i);
            if(categoryVo!=null){
                categoryIds.add(categoryVo.getId());
                getCategoryIds(categoryVo.getChildCategory(),categoryIds);
            }
        }
    }

}
